﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlTypes;
using Logic;
using Helper;
using DBAccess;
using WHUT;

public partial class token_work_list : WorkBasePage
{
    IPagedDataProvider pdp = PagedDataProviderFactory.Instance();

    readonly static AbstractLogger log = AbstractLogger.GetLogger(typeof(token_work_list));

    protected void Page_Load(object sender, EventArgs e)
    {
        pdp.ConnectionName = Common.GetDefaultSqlConnectionName();
        pdp.TableOrView = "t_token_work";
        pdp.FieldsToReturn = "id,name,style,date_add,tool,url,pic_img";
        //pdp.FieldsToReturn = "*";
        pdp.FieldToOrder = "id";
        pdp.Ascending = false;
        pdp.PageSize = spPager.PageSize;
        pdp.RecordsFilter = "1=1";
         
        if (!IsPostBack)
        {
            BindWorks(true);
        }
    }

    private void BindWorks(bool bResetPager)
    {
        if (bResetPager)
        {
            spPager.CurrentPageIndex = 1;
            spPager.RecordsCount = pdp.TotalRecords;
        }

        pdp.CurrentPageIndex = spPager.CurrentPageIndex;
        gvWorks.DataSource = pdp.GetCurrentPageAsDataTable().DefaultView;
        gvWorks.DataBind();
    }

    /// <summary>
    /// 翻页控制
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SimplePager1_PageIndexChanging(object sender, wbh.PageChangingEvent e)
    {
        spPager.CurrentPageIndex = e.NewIndex;
        BindWorks(false);
    }

    protected void gvWorks_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "del")
        {
            string res = Works.Delete(int.Parse(e.CommandArgument.ToString()));
            if (null != res)
            {
                lbTips.Text = "操作失败。" + res;
                lbTips.Visible = true;
                log.Error(lbTips.Text);
                return;
            }
            
            // rebind grid
            BindWorks(false);
        }
    }

}
